package com.uam.web.common.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;


@Data
@Builder
@ApiModel(value = "响应主体类")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "响应状态码，200：成功  101：失败")
    private int code;
    @ApiModelProperty(value = "响应状态信息")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 构造方法
     */
    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Result(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(int code) {
        this.code = code;
    }

    /**
     * 响应成功
     *
     * @param msg  返回信息
     * @param data 泛型数据
     * @param <T>  返回数据，能够不填
     * @return 1.状态码（默认） 2.返回信息 3.泛型数据
     */
    public static <T> Result<T> createSuccess(String msg, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> createSuccess(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), data);
    }

    public static <T> Result<T> createSuccess(String msg) {
        return new Result<>(ResultCode.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> createSuccess() {
        return new Result<>(ResultCode.SUCCESS.getCode());
    }

    /**
     * 响应失败
     *
     * @param code
     * @param msg
     * @return 1.状态码（自定义） 2.返回信息（自定义）
     */
    public static <T> Result<T> createError(int code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> createError() {
        return new Result<>(ResultCode.ERROR.getCode(), ResultCode.ERROR.getDesc());
    }

    public static <T> Result<T> createError(String msg) {
        return new Result<>(ResultCode.ERROR.getCode(), msg);
    }


    public static <T> Result<T> ok() {
        return restResult(null, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc());
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc());
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, ResultCode.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> fail() {
        return restResult(null, ResultCode.ERROR.getCode(), ResultCode.ERROR.getDesc());
    }

    public static <T> Result<T> fail(String msg) {
        return restResult(null, ResultCode.ERROR.getCode(), msg);
    }

    public static <T> Result<T> fail(T data) {
        return restResult(data, ResultCode.ERROR.getCode(), ResultCode.ERROR.getDesc());
    }

    public static <T> Result<T> fail(T data, String msg) {
        return restResult(data, ResultCode.ERROR.getCode(), msg);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public boolean isSuccess() {
        return ResultCode.SUCCESS.getCode() == this.code;
    }

    /**
     * 调用返回自定义的信息，无数据返回前端
     *
     * @param <T>
     * @date 2019/10/10 17:37
     * @modify heguanglong
     */
    public static <T> Result<T> build(int status, String msg) {
        Result rs = new Result<>();
        rs.code = status;
        rs.setMsg(msg);
        return rs;
    }

    public Result() {
    }

    public Result(ResultCode resultCode) {
        Result rs = new Result<>();
        rs.setCode(resultCode.getCode());
        rs.setMsg(resultCode.getDesc());
    }

    public static Result error(ResultCode resultCode) {
        return new Result(resultCode);
    }

    /**
     * @param errMsg
     */
    public static Result errorMsg(String errMsg) {
        return new Result(ResultCode.ERROR.getCode(), errMsg);
    }

    /**
     * 参数有误
     *
     * @param errMsg
     */
    public static Result paramError(String errMsg) {
        return new Result(ResultCode.ILLEGAL_ARGUMENT.getCode(), ResultCode.ILLEGAL_ARGUMENT.getDesc() + " " + errMsg);
    }

    public static Result fileTypeError(String msg) {
        return new Result(ResultCode.FILE_TYPE_ERROR.getCode(), ResultCode.FILE_TYPE_ERROR.getDesc() + " " + msg);
    }
     public static Result successPage(String msg) {
        return new Result(ResultCode.FILE_TYPE_ERROR.getCode(), ResultCode.FILE_TYPE_ERROR.getDesc() + " " + msg);
    }

}
